const to = (promise) => {
    return new Promise(resolve => {
        promise.then(res => {
            resolve([null, res])
        }).catch(err => {
            console.log(err)
            resolve([err, null])
        })
    })
}

const reverseTcpData = (data) => {
    data.forEach((item, index) => {
        if(index % 2 === 1) [data[index], data[index - 1]] = [data[index - 1], data[index]]
    })
}

//boolean = true高低位不翻转  false高低位翻转
const to10Hex = (data, boolean) => {
    let arr = []
    data.forEach((item, index) => {
        if(index % 2 === 1){
            let before = data[index - 1].toString(16)
            let after = item.toString(16)
            before = before.length === 2 ? before : '0' + before
            after = after.length === 2 ? after : '0' + after
            if(boolean) [before, after] = [after, before]
            arr.push(Number('0x' + (after + before)))
        }
    })
    return arr
}

const jsonCopy = (obj) => {
    return JSON.parse(JSON.stringify(obj))
}

module.exports = { to, reverseTcpData, to10Hex, jsonCopy }